package com.zhangmengyuan.medical.controller;

import com.zhangmengyuan.medical.entity.Goods;
import com.zhangmengyuan.medical.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    public String save(Goods goods, Model model) {
        this.goodsService.save(goods);
        return "redirect:/goods/all";
    }

    public String update(Goods goods, @PathVariable("goodsId") Integer goodsId, Model model) {
        goods.setGoodsId(goodsId);
        this.goodsService.save(goods);
        return "redirect:/goods/all";
    }

    public String getGoods(Model model) {
        Page<Goods> goodsList = this.goodsService.getAll(1);
        model.addAttribute("goodsList", goodsList);
        return "allgoods";
    }

    @GetMapping("/{goodsId}")
    public String getGoodsById(@PathVariable("goodsId") Integer goodsId, Model model) {
        Goods goods = this.goodsService.getById(goodsId);
        model.addAttribute("goods", goods);
        return "allgoods";

    }

    public String toDetil(Model model) {
        return "allgoods";
    }
}
